package com.loe.ktui.ui_custom

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import com.loe.ktui.dist
import com.loe.ktui.ui.KtTextView
import com.loe.ktui.ui.UiGroup
import com.loe.ktui.ui.UiText
import com.loe.ktui.util.KtColor

class UiCheckText(id: String? = null) : UiText(id)
{
    var checkImage0: Any? = null
    var checkImage1: Any? = null
    var checkImage2: Any? = null

    var checkColor0: Int? = null
    var checkColor1: Int? = null
    var checkColor2: Int? = null

    override var imageTop: Any? = 0.5
    override var imageWidth: Any? = 13
    override var imageHeight: Any? = 13
    override var imagePadding: Any? = 5

    var isCheckEnable: Boolean? = null
    var isCheck: Boolean = false

    var onCheck: (KtCheckText.(isCheck: Boolean) -> Unit)? = null

    open fun initUI(context: Context, view: KtCheckText)
    {
        super.initUI(context, view)

        checkImage0?.let { view.checkImage0 = it }
        checkImage1?.let { view.checkImage1 = it }
        checkImage2?.let { view.checkImage2 = it }
        checkColor0?.let { view.checkColor0 = it }
        checkColor1?.let { view.checkColor1 = it }
        checkColor2?.let { view.checkColor2 = it }

        imageTop?.let { view.imageTop = it.dist }
        imageWidth?.let { view.imageWidth = it.dist }
        imageHeight?.let { view.imageHeight = it.dist }
        imagePadding?.let { view.imagePadding = it.dist }

        view.onCheck = null
        isCheckEnable?.let { view.isCheckEnable = it }
        view.isCheck = isCheck
        onCheck?.let { onCheck -> view.setOnCheck { view.onCheck(it) } }

    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtCheckText
    {
        return KtCheckText(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_CheckText(id: String? = null, block: (UiCheckText.() -> Unit)? = null): UiCheckText
{
    val ui = UiCheckText(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

class KtCheckText : KtTextView
{
    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(attrs)
    }

    private fun initXML(attrs: AttributeSet? = null)
    {
        setOnClickListener()
        {
            if (isCheckEnable)
            {
                toggle()
            }
        }
    }

    /********************************************************************************************/

    internal var checkImage0: Any = "kt_icon_check_0"
    internal var checkImage1: Any = "kt_icon_check_1"
    internal var checkImage2: Any = "kt_icon_check_2"

    internal var checkColor0 = KtColor.textLight
    internal var checkColor1 = KtColor.main
    internal var checkColor2 = KtColor.gray

    fun toggle()
    {
        isCheck = !isCheck
    }

    fun notifyCheck()
    {
        if (isCheckEnable)
        {
            if (isCheck)
            {
                image = checkImage1
                imageColor = checkColor1
            } else
            {
                image = checkImage0
                imageColor = checkColor0
            }
        } else
        {
            if (isCheck)
            {
                image = checkImage2
                imageColor = checkColor2
            } else
            {
                image = checkImage0
                imageColor = checkColor0
            }
        }
        notifyImage()
    }

    var isCheckEnable = true
        set(value)
        {
            field = value
            notifyCheck()
        }

    var isCheck = false
        set(value)
        {
            field = value
            notifyCheck()
            onCheck?.invoke(isCheck)
        }

    internal var onCheck: ((isCheck: Boolean) -> Unit)? = null

    fun setOnCheck(onCheck: (isCheck: Boolean) -> Unit)
    {
        this.onCheck = onCheck
    }
}